perm filename SUPDUP.C[NET,MRC] blob sn#341867 filedate 1978-03-18 generic text, type T, neo UTF8
#
/* supdup user - see rfc 734 */

#include "open.h"
#define SIGINR 15
#define CONTIMOUT 120           /* number of seconds to wait */
#define BUFSIZ 100

#define TNSFW   7L

#define TOALT   0200000L
#define TOERS   040000L
#define TOMVB   010000L
#define TOSAI   04000L
#define TOOVR   01000L
#define TOMVU   0400L
#define TOLWR   020L
#define TOFCI   010L
#define TOLID   02L
#define TOCID   01L
#define TPCBS   040L
#define TPORS   010L
#define TOCLC   0100000L
#define TOSA1   02000L
#define TOMOR   0200L
#define TOROL   0100L

#define TDMOV   0200
#define TDMV1   0201
#define TDEOF   0202
#define TDEOL   0203
#define TDDLF   0204
#define TDCRL   0207
#define TDNOP   0210
#define TDORS   0214
#define TDQOT   0215
#define TDFS    0216
#define TDMV0   0217
#define TDCLR   0220
#define TDBEL   0221
#define TDILP   0223
#define TDDLP   0224
#define TDICP   0225
#define TDDCP   0226
#define TDBOW   0227
#define TDRST   0230

int nf;                  /* net file descriptor */
char loc[] "\300\302Santa Monica";

struct
{       int fildes;
	int nleft;
	char *nextp;
	char buff[512];
⎇       ninbuf[1], toutbuf[1];


main(argc,argv)
int argc;
char *argv[];
{
	int newpid;
	int i,j;
	int hnum;
	int ttyflags[3], oldflags;
	char buf[BUFSIZ];

	openparam.o←fskt[1] = 0137;     /* special SUPDUP socket */
	openparam.o←timo = CONTIMOUT;
	hnum = 236;                     /* default to MC */
	if(argc > 1)
	{       if (argc > 2)
			openparam.o←fskt[1] = atoi(argv[2]);
		if ( (hnum = atoi(hnconv(argv[1]))) == 0)
			printf("Unknown host name\n");
	⎇

/*      while (hnum == 0)
	{
	    printf("Host: ");
	    if(read(0,ttibuf,30) <= 0) exit();
	    if (ttibuf[0]=='?')
		showhsts();
	    else
	    {   if ( (hnum = atoi(hnconv(ttibuf))) == 0)
			printf("Unknown host name\n");
	    ⎇
	⎇
*/
	openparam.o←host = hnum;
	signal(SIGINR,1);
	nf = open("/dev/net/anyhost",&openparam);
	if (nf < 0)
	{       printf("Cannot connect\n");
		exit();
	⎇
	ninbuf[0].fildes = nf;
	toutbuf[0].fildes = 1;
	printf("Open\n");
	sendopt();
	gtty(0,ttyflags);
	oldflags = ttyflags[2];
	ttyflags[2] =& }000032;
	ttyflags[2] =|  000040;
	stty(0,ttyflags);
	newpid = fork();
	if (newpid == 0)
		output();
	else                                            /* this is parent */
	{       while ((j=read(0,buf,BUFSIZ)) > 0)
		{       for (i=0; i<j;i++)
			{       if (buf[i] == '\034')
					goto end;
			⎇
			if (write(nf,buf,j) < 0)
				break;
		⎇
end:            kill(newpid,9);
		ttyflags[2] = oldflags;
		stty(0,ttyflags);
	⎇
⎇


sendopt()
{
	out36(-5l,0l);          /* parameter count */
	out36(0l,TNSFW);
	out36(TOMVB+TOMVU+TOLWR+TOMOR,TPCBS);   /* options */
	out36(0l,40l);          /* height */
	out36(0l,79l);          /* width */
	out36(0l,1l);           /* glitch count */
	write(nf,loc,sizeof loc);
⎇


out36(hi,lo)
long int hi,lo;
{
	char bytes[6];

	bytes[0] = (hi>>12) & 077;
	bytes[1] = (hi>> 6) & 077;
	bytes[2] = (hi    ) & 077;
	bytes[3] = (lo>>12) & 077;
	bytes[4] = (lo>> 6) & 077;
	bytes[5] = (lo    ) & 077;
	write(nf,bytes,6);
⎇


output()
{
	register int c;

	while ((c = getc(ninbuf)) >= 0)
	{       if (c < 0200)
			putc(c,toutbuf);
		else
			switch(c)
			{   case TDMOV:
				getc(ninbuf);
				getc(ninbuf);
				goto cursor;
				break;
			    case TDMV1:
				goto cursor;
			    case TDEOF:
			    case TDEOL:
				break;
			    case TDDLF:
				putc(' ',toutbuf);
				putc('\b',toutbuf);
				break;
			    case TDCRL:
				putc('\r',toutbuf);
				putc('\n',toutbuf);
				break;
			    case TDNOP:
				break;
			    case TDORS:
				break;
			    case TDQOT:
				c = getc(ninbuf);
				putc(c,toutbuf);
				break;
			    case TDFS:
				putc('\037',toutbuf);
				break;
			    case TDMV0:
			    cursor:
				pcursor(getc(ninbuf),getc(ninbuf));
				break;
			    case TDCLR:
				putc('\014',toutbuf);
				putc('\020',toutbuf);
				break;
			    case TDBEL:
				putc('\007',toutbuf);
				break;
			    case TDILP:
			    case TDDLP:
			    case TDICP:
			    case TDDCP:
				getc(ninbuf);
			    case TDBOW:
			    case TDRST:
				break;
			⎇
		if (ninbuf[0].nleft == 0)
			fflush(toutbuf);
	⎇
⎇


pcursor(col,lin)
int col,lin;
{
	putc('\017',toutbuf);
	putc( (col > 0 ? (col/10)*020 + col % 10 : 0177), toutbuf);
	putc( (lin < 20 ? 0100 + lin : 0140 + (lin-20)), toutbuf);
⎇